# -*- coding: utf-8 -*-
"""
Created on Sat Mar 27 13:37:44 2021

@author: Apple
"""


import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from goodreads import Ui_MainWindow
import requests
from lxml import etree

class MainWindow(QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
    def transCityName(self,cityName):
        cityCode = ''
        if cityName == 'romance':
            cityCode = 'romance'
        elif cityName == 'horror':
            cityCode = 'horror'
        elif cityName =='sci-fi':
            cityCode = 'sci-fi'
        elif cityName == 'historical':
            cityCode = 'historical'
        elif cityName =='humorous':
            cityCode = 'humorous'        
        return cityCode    
        
    def queryWeather(self):
        print("*queryWeather")
        cityName = self.ui.comboBox.currentText()
        cityCode = self.transCityName(cityName)
        
        url = 'https://www.goodreads.cc/'+cityCode+'/lastupdate_1.html'
        headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'}
        data = requests.get(url,headers=headers).text
        f = etree.HTML(data)
        books = f.xpath('//*[@id="diyax3"]/article/header/h2/a/text()') 
        authors = f.xpath('//*[@id="diyax3"]/article/p[1]/a/text()')
        notes = f.xpath('//*[@id="diyax3"]/article/p[3]/text()')
        # msg1 = books[0] + '\n'
        # msg2 = authors[0] + '\n'
        # msg3 = notes[0] + '\n'
        # msg4 = books[1] + '\n'
        # msg5 = authors[1] + '\n'
        # msg6 = notes[1] + '\n'
        # result = msg1+msg2+msg3+msg4+msg5+msg6
        result = ''
        for j in range(20):
            result += books[j]+'\n'+authors[j]+'\n'+notes[j]+'\n'
                
        self.ui.textEdit.setText(result)
        
        
    def clearResult(self):
        print("* clearResult")
        self.ui.textEdit.clear()

            
if __name__=="__main__":
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())